package tests;

import static org.junit.Assert.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.junit.Test;
import negocios.Minibook;
import datos.BackUp;

public class BackUpTest{
	
	public static void persistenciaGuardar(Minibook miniOut){
		try{
			FileOutputStream fos = new FileOutputStream("test.txt");
			ObjectOutputStream out = new ObjectOutputStream(fos);
			out.writeObject(miniOut);
			out.close();
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
	}

	public static Minibook persistenciaRecuperar(){
		Minibook miniIN = null;
		try{
			FileInputStream fis = new FileInputStream("test.txt");
			ObjectInputStream in = new ObjectInputStream(fis);			
			miniIN = (Minibook) in.readObject();	
			in.close();
		}
		catch (Exception ex){
			ex.printStackTrace();
		}
		return miniIN;
	}
	
	
	
	
	@Test
	public void persistenciaGuardarTest(){
		
		Minibook mini = Minibook.inicializarMinibook();
		
		mini.agregarUsuario("Oscar","oscar@.com",45);
		mini.agregarUsuario("juan","juan@.com.ar",45);
		mini.agregarUsuario("juana","juana@.com",33);
		mini.agregarUsuario("pedro","pedro@.com",26);
		mini.agregarUsuario("laura","laura@.com",18);

		mini.agregarAmistad("oscar@.com", "juana@.com");
		mini.agregarAmistad("laura@.com", "juana@.com");
		mini.agregarAmistad("laura@.com", "pedro@.com");
		
		persistenciaGuardar(mini);
	}
	
	@Test
	public void persistenciaRecuperarTest(){
		
		Minibook otro = persistenciaRecuperar();
		
		assertTrue(otro.estaRegistrado("laura@.com"));
		assertTrue(otro.estaRegistrado("pedro@.com"));
		assertTrue(otro.estaRegistrado("juana@.com"));
		assertTrue(otro.estaRegistrado("juan@.com.ar"));
		assertTrue(otro.estaRegistrado("oscar@.com"));
		
		assertTrue(otro.sonAmigos("oscar@.com", "juana@.com"));
		assertTrue(otro.sonAmigos("laura@.com", "pedro@.com"));
		assertTrue(otro.sonAmigos("juana@.com", "laura@.com"));
		
		assertFalse(otro.sonAmigos("oscar@.com", "juan@.com.ar"));
		
		assertTrue(otro.listaDeAmigos("oscar@.com").contains(otro.getPersona("juana@.com")));
		assertTrue(otro.listaDeAmigos("laura@.com").contains(otro.getPersona("juana@.com")));
		assertTrue(otro.listaDeAmigos("laura@.com").contains(otro.getPersona("pedro@.com")));
		
		assertTrue(otro.listaDeAmigos("juana@.com").contains(otro.getPersona("oscar@.com")));
		assertTrue(otro.listaDeAmigos("juana@.com").contains(otro.getPersona("laura@.com")));
		
		assertFalse(otro.listaDeAmigos("oscar@.com").contains(otro.getPersona("pedro@.com")));
		
		assertTrue(otro.getCantidadDeAmigosDe("laura@.com") == 2);
		assertTrue(otro.getCantidadDeAmigosDe("oscar@.com") == 1);
		assertTrue(otro.getCantidadDeAmigosDe("juana@.com") == 2);
		assertTrue(otro.getCantidadDeAmigosDe("pedro@.com") == 1);
		assertTrue(otro.getCantidadDeAmigosDe("juan@.com.ar") == 0);
		
		
		
		
		
	}

}
